const tree = {
    a: ['b', 'c'],
    b: ['e'],
    e: ['f']
};
const keys = {};
for (let k in tree) {
    keys[k] = true;
    for (let ck of tree[k]) {
        keys[ck] = true;
    }
}

const getNodeAsync = id => new Promise((resolve)=> {
        setTimeout(()=> {
            let node=tree[id] ? {id,children:tree[id]||[]}:null;
            //console.log('getNode:',id,node);
            resolve(node);
        });
    });

const getTreeAsync =async id =>{
    let node=await getNodeAsync(id);
    if(!node)return {id,children:[]};


    return {
        id,
        children:await (async ()=>{
            let node=await getNodeAsync(id);
            return Promise.all(node.children.map(getTreeAsync));
        })()
    };
};


(async ()=>{
    let tree=await getTreeAsync('a');
    console.log('tree:\n',JSON.stringify(tree,null,2));
})();



//const getNodeAsync
/*
 //const getTreeAsync
 第一步的答案写在这里
 第二步
 改写 getNode 为异步的 getNodeAsync （ callback,
 promise, generator 等方式皆可）。
 例子(callback)
 function	getNodeAsync(id,	cb)	{
 process.nextTick(cb.bind(null,	null,	getNode(id)));
 }
 并利用 getNodeAsync 实现 getTreeAsync。
 可以使用 async,bluebird,co 等。*/
